package cn.iocoder.yudao.module.member.service.scoretype;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.scoretype.vo.ScoreTypeCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scoretype.vo.ScoreTypeExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scoretype.vo.ScoreTypePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scoretype.vo.ScoreTypeUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.scoretype.ScoreTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scoretype.ScoreTypeDO;
import cn.iocoder.yudao.module.member.dal.mysql.scoretype.ScoreTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SCORE_TYPE_NOT_EXISTS;

/**
 * 积分用途 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ScoreTypeServiceImpl implements ScoreTypeService {

    @Resource
    private ScoreTypeMapper scoreTypeMapper;

    @Override
    public Long createScoreType(ScoreTypeCreateReqVO createReqVO) {
        // 插入
        ScoreTypeDO scoreType = ScoreTypeConvert.INSTANCE.convert(createReqVO);
        scoreTypeMapper.insert(scoreType);
        // 返回
        return scoreType.getId();
    }

    @Override
    public void updateScoreType(ScoreTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validateScoreTypeExists(updateReqVO.getId());
        // 更新
        ScoreTypeDO updateObj = ScoreTypeConvert.INSTANCE.convert(updateReqVO);
        scoreTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteScoreType(Long id) {
        // 校验存在
        validateScoreTypeExists(id);
        // 删除
        scoreTypeMapper.deleteById(id);
    }

    private void validateScoreTypeExists(Long id) {
        if (scoreTypeMapper.selectById(id) == null) {
            throw exception(SCORE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ScoreTypeDO getScoreType(Long id) {
        return scoreTypeMapper.selectById(id);
    }

    @Override
    public List<ScoreTypeDO> getScoreTypeList(Collection<Long> ids) {
        return scoreTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ScoreTypeDO> getScoreTypePage(ScoreTypePageReqVO pageReqVO) {
        return scoreTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScoreTypeDO> getScoreTypeList(ScoreTypeExportReqVO exportReqVO) {
        return scoreTypeMapper.selectList(exportReqVO);
    }

}
